package main

import (
	"fmt"
	"net"
)

func main() {
	//监听udp连接信息
	listen, err := net.ListenUDP("udp", &net.UDPAddr{
		IP:   net.IPv4(127, 0, 0, 1),
		Port: 40000,
	})
	if err != nil {
		fmt.Println("conn failed err:", err)
		return
	}
	defer listen.Close()
	var data [4]byte //存放读取的数据
	//var data = make([]byte, 4)
	for {
		//读取收到的udp数据放入data,并返回客户端地址addr
		n, addr, err := listen.ReadFromUDP(data[:])
		if err != nil {
			fmt.Println("read failed err:", err)
			continue
		}
		fmt.Printf("收到数据：n:%d , addr:%v ,data:%v ,data2:%v\n", n, addr, data[0:n], string(data[0:n]))

		_, err = listen.WriteToUDP(data[0:n], addr)
		if err != nil {
			fmt.Println("WriteToUDP failed,err:", err)
		}
	}

}
